import FileController from './file'
import { Context } from '../type'
import { pubsub } from '../index'

export default {
  Query: {
    hello: (parent, { name }, context, info) => `Hello ${name}!`,
    list: async (parent, { path }, context, info) => {
      return await FileController.list(path)
    },
    openFile: (parent, { path }, context, info) =>
      FileController.openFile(path),
    path: (parent, { paths }, context, info) => FileController.path(paths),
  },
  Subscription: {
    list: {
      subscribe: async (
        parent,
        { path },
        { connection: { context } }: { connection: { context: Context } }
      ) => {
        FileController.watch(path, context)
        return pubsub.asyncIterator(context.channel)
      },
    },
  },
}
